package dfs;

/**
 * @author 帅小伙
 * @date 2022/1/1
 * @description
 */
public class Demo02ZJ79是否是平衡二叉树 {


    boolean flag = true;
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root==null){
            return true;
        }

        deep(root);
        return flag;

    }


    public int deep(TreeNode node){
        if(node==null){
            return 1;
        }
        if(Math.abs(deep(node.right)-deep(node.left))>1){
            flag = false;
            return 0;
        }
        return Math.max(deep(node.right),deep(node.left))+1;
    }


    public boolean IsBalanced_Solution1(TreeNode root) {
        if(root==null){
            return true;
        }

        return Math.abs(deep1(root.left)-deep1(root.right)) <=1 &&
                IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);

    }


    public int deep1(TreeNode node){
        if(node==null){
            return 0;
        }

        return Math.max(deep(node.right),deep(node.left))+1;
    }

    class TreeNode{
        public TreeNode right;
        public TreeNode left;
        public int var;
    }

}
